import { useEntityManager, useRedis, useTransaction } from "@sker/core";
import { ITask } from "./tokens";
import { AutoDeviceWork, AutoTask } from "@sker/entities";
import dayjs from "dayjs";
export class HealthTask implements ITask {
    name: string = `HealthTask`;
    title: string = `养机任务`;
    order: number = 0;
    delay: number = 1000 * 10;
    async getCode() {
        return await useEntityManager(async (m) => {
            const task = await m.findOne(AutoTask, { where: { name: this.name } })
            return {
                code: task.code,
                id: this.name,
                loopTimes: 1,
                interval: 0
            }
        })
    }
    async start(payload: {uuid: string, event: string}) {
        return await useTransaction(async m => {
            const date = dayjs().format('YYYY-MM-DD')
            const deviceWork = await m.findOne(AutoDeviceWork, { where: { date: date, uuid: payload.uuid } })
            if (!deviceWork) return;
            await m.update(AutoDeviceWork, deviceWork.id, { status: `1` })
            await useRedis(5, async r => {
                await r.set(payload.uuid, `1`)
            });
            return { type: `HEALTH_TASK_START`, payload: { pkg: `com.smile.gifmaker`, keyword: `美女` } }
        })
    }
    isFinishSign(event: string) {
        return event === `HEALTH_TASK_FINISH`
    }
    async finish(uuid: string): Promise<any> {
        return await useTransaction(async m => {
            const date = dayjs().format('YYYY-MM-DD')
            const deviceWork = await m.findOne(AutoDeviceWork, { where: { date: date, uuid: uuid } })
            if (!deviceWork) return;
            await m.update(AutoDeviceWork, deviceWork.id, { status: `0` })
            await useRedis(5, async r => {
                await r.set(uuid, `0`)
            });
            return { type: this.name, uuid: uuid }
        })
    }
    canRun() {
        const hour = dayjs().get('hour')
        const minute = dayjs().get('minute')
        return hour === 17 && minute < 59;
    }
}
